import "package:flutter/material.dart";

// 缓存内容
class KeepAliveWrapper extends StatefulWidget {
  bool keepAlive;
  Widget child;
  KeepAliveWrapper({super.key, this.keepAlive = true, required this.child});
  @override
  State<StatefulWidget> createState() => KeepAliveWrapperState();
}

class KeepAliveWrapperState extends State<KeepAliveWrapper>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return widget.child;
  }

  @override
  void didUpdateWidget(covariant KeepAliveWrapper oldWidget) {
    // 如果传入的keepAlive状态改变，则更新keepAlive
    if (widget.keepAlive != oldWidget.keepAlive) {
      updateKeepAlive();
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  bool get wantKeepAlive => widget.keepAlive;
}
